﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.IO;
using System.Threading;
using WatiN.Core;
using WatiN.Core.Interfaces;
using WatiN.Core.UnitTests;

namespace WatiN.Samples.Console
{
    public class Program : CrossBrowserTest
    {
        [STAThread]
        static void Main(string[] args)
        {
            using (Program program = new Program())
            {
                // XiaoNei website
                ReadConfig();

                string[] filelist = Directory.GetFiles(_appDirectory, "*.ini");
                Array.Sort(filelist);
                foreach (string filename in filelist)
                {
                    BaseUserConfigData userConfig = ConfigHelper.ReadUserConfigData(filename);
                    program.SearchForWatiNOnXiaoNei(userConfig);
                }
            }
        }

        public void GoXiaoNei(string url, IBrowser browser)
        {
            GoTo(url, browser);
            //browser.Link(Find.ByUrl(url)).Click();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="config"></param>
        private void SearchForWatiNOnXiaoNei(BaseUserConfigData config)
        {
            using (IBrowser ie = BrowserFactory.Create(BrowserType.InternetExplorer))
            {
                string result = String.Empty;

                //Step-1: login
                GoTo("http://www.xiaonei.com", ie);
                ie.TextField(Find.ByName("email")).Value = config.Email;
                ie.TextField(Find.ByName("password")).Value = config.Password;
                //ie.CheckBox(Find.ByName("autoLogin")).Checked = false;
                ie.Button(Find.ByValue("登录")).Click();
                //GoTo("http://login.xiaonei.com/Login.do?email=" + email + "&password=" + password + "&origURL=http%3A%2F%2Fwww.xiaonei.com%2FSysHome.do", ie);
                result = ie.Text;
                if (!ie.Title.StartsWith("校内网 - "))
                    return;
                Thread.Sleep(ConfigHelper.SLEEP_TIME);

                //Step-2: car parking
                //ie.Link(Find.ByUrl("http://apps.xiaonei.com/carpark/")).Click();
                GoTo("http://apps.xiaonei.com/carpark/", ie);
                result = ie.Text;
                if (!ie.Title.Equals("校内网"))
                    return;

                ParkerContent pct = new ParkerContent();
                pct.GetFriendList(ie.Html);

                GoTo("http://carpark.xiaonei.com/ajaxGetMyCar.f", ie);
                // 1. only one car
                //{"carlist":[{"park_type":0,
                //             "user_id":4418450,
                //             "user_name":"\u55bb\u6e14",
                //             "car_style":1,
                //             "car_id":1,
                //             "car_icon":"lingmu0",
                //             "money":"2150",
                //             "park_site_id":17797405,
                //             "uc_id":18126467,
                //             "car_name":"\u4e8c\u624b\u5965\u62d3"}]}
                // 2. two or more car
                //{"carlist":[{"park_type":0,"user_id":4418080,"user_name":"\u6c64\u8001\u5e08","car_style":1,"car_id":1,"car_icon":"lingmu0","money":"290","park_site_id":17795923,"uc_id":18126467,"car_name":"\u4e8c\u624b\u5965\u62d3"},
                //            {"park_type":0,"user_id":4418080,"user_name":"\u6c64\u8001\u5e08","car_style":20,"car_id":2,"car_icon":"lingmu0","money":"290","park_site_id":17795925,"uc_id":18481934,"car_name":"\u90fd\u5e02\u8d1d\u8d1d"}]} 
                string carinfo = ie.Text;
                pct.GetCarlist(carinfo);

                foreach (CarInfo car in pct._carInfoList)
                {
                    pct.DoParker(car, ie, this);
                }

                GoTo("http://www.xiaonei.com/Logout.do", ie);
            }

        }

        /// <summary>
        /// 
        /// </summary>
        private static void ReadConfig()
        {
            string filename = _appDirectory + Path.DirectorySeparatorChar + CONFIG_FILENAME;

            if (!File.Exists(filename))
            {
                //TraceLog.PrintLn("缺少{0}文件，系统加载默认配置.", filename);
                return;
            }

            //_config = ConfigHelper.ReadGlobalConfigData(filename);
        }

        #region Member Fields
        private const string CONFIG_FILENAME = "Config.txt";
        private static string _appDirectory = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
        //private static ConfigHelper _config = new ConfigHelper();
        #endregion
    }
}
